package com.lc.guessTheWords.core;

import java.util.HashMap;

import com.google.gson.reflect.TypeToken;
import com.lc.guessTheWords.entity.MapInfo;
import com.lc.guessTheWords.entity.StageData;
import com.lc.guessTheWords.util.GsonHelper;
import com.lc.guessTheWords.util.ShareSettingUtil;
import com.lc.guessTheWords.util.WordMapLoader;
import com.wiyun.engine.nodes.Director;

public class StageManager {
	public static StageManager instance = new StageManager();

	private StageManager() {
	}

	private static final String STAGE_DATA_KEY = "stageDataKey";
	private static final String LAST_STAGE_KEY = "lastStageKey";
	private static final String LAMP_COUNT_KEY = "lampCountKey";
	private static final String GOLD_COUNT_KEY = "goldCountKey";
	private static final String ISBUY_ALL_STAGES_KEY = "isbuyAllStagesKey";
	private static final String LAST_STAGE_COUNT_KEY = "lastStageCountKey";

	// 第一次显示游戏商城用的
	private static final String IS_SHOWED_SHOP = "isfirstshowshop";

	public HashMap<Integer, StageData> stageDatas;
	public HashMap<Integer, MapInfo> mapInfos;
	private int lampCount;
	private int lastStage;
	private int goldCount;
	private boolean isBuyAllStages;
	private boolean isShowedShop;
	private int lastStageCount;

	/**
	 * 上一版本游戏的关卡数目
	 * 
	 * @return
	 */
	public int getLastStageCount() {
		return lastStageCount;
	}

	public void setLastStageCount(int lastStageCount) {
		this.lastStageCount = lastStageCount;
		ShareSettingUtil.storeIntSetting(LAST_STAGE_COUNT_KEY, lastStageCount);
	}

	public boolean isShowedShop() {
		return isShowedShop;
	}

	public void setShowedShop(boolean isShowedShop) {
		this.isShowedShop = isShowedShop;
		ShareSettingUtil.storeBooleanSetting(IS_SHOWED_SHOP, isShowedShop);
	}

	public boolean isBuyAllStages() {
		return isBuyAllStages;
	}

	public void setBuyAllStages(boolean isBuyAllStages) {
		this.isBuyAllStages = isBuyAllStages;
		ShareSettingUtil.storeBooleanSetting(ISBUY_ALL_STAGES_KEY,
				isBuyAllStages);
	}

	public int getGoldCount() {
		return goldCount;
	}

	public void setGoldCount(int goldCount) {
		this.goldCount = goldCount;
		ShareSettingUtil.storeIntSetting(GOLD_COUNT_KEY, goldCount);
	}

	public int getLampCount() {
		return lampCount;
	}

	public void setLampCount(int lampCount) {
		this.lampCount = lampCount;
		ShareSettingUtil.storeIntSetting(LAMP_COUNT_KEY, lampCount);
	}

	public int getLastStage() {
		return lastStage;
	}

	public void setLastStage(int lastStage) {
		this.lastStage = lastStage;
		ShareSettingUtil.storeIntSetting(LAST_STAGE_KEY, lastStage);
	}

	public void saveStageDatas() {
		ShareSettingUtil.storeStringSetting(STAGE_DATA_KEY,
				GsonHelper.getJsonString(stageDatas));
	}

	@SuppressWarnings("unchecked")
	public void initStageData() {

		// 读取关卡信息
		mapInfos = new HashMap<Integer, MapInfo>();
		for (int i = 1; i <= GameSystem.stageCount; i++) {
			MapInfo info = WordMapLoader.loadMap(i);
			mapInfos.put(i, info);
		}

		// 载入完成情况
		String value = ShareSettingUtil.getStringSetting(STAGE_DATA_KEY);
		if (!value.equalsIgnoreCase("null")) {
			stageDatas = (HashMap<Integer, StageData>) GsonHelper
					.getObjectbyJsonString(value,
							new TypeToken<HashMap<Integer, StageData>>() {
							}.getType());
		} else {
			// 初始化
			stageDatas = new HashMap<Integer, StageData>();
			stageDatas.put(1, new StageData());
			stageDatas.put(2, new StageData());
			stageDatas.put(3, new StageData());
			saveStageDatas();
		}

		lastStage = ShareSettingUtil.getIntSetting(LAST_STAGE_KEY, 0);
		lampCount = ShareSettingUtil.getIntSetting(LAMP_COUNT_KEY, 2);
		// 启动获得20金币
		goldCount = ShareSettingUtil.getIntSetting(GOLD_COUNT_KEY, 20);
		isBuyAllStages = ShareSettingUtil.getBooleanSetting(
				ISBUY_ALL_STAGES_KEY, false);
		isShowedShop = ShareSettingUtil
				.getBooleanSetting(IS_SHOWED_SHOP, false);
		lastStageCount = ShareSettingUtil.getIntSetting(LAST_STAGE_COUNT_KEY,
				25);
	}

	/**
	 * 获取当前关卡完成情况
	 * 
	 * @return
	 */
	public StageData getCurrentStageData() {
		return stageDatas.get(lastStage);
	}

	/**
	 * 解锁所有关卡
	 */
	public void unlockAllStages() {
		for (int i = 4; i <= GameSystem.stageCount; i++) {
			if (!StageManager.instance.stageDatas.containsKey(i)) {
				StageManager.instance.stageDatas.put(i, new StageData());
			}
		}
		Director.getInstance().runThread(new Runnable() {
			@Override
			public void run() {
				saveStageDatas();
			}
		});
	}

}
